<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html>
<head>
    <title>菜品管理</title>
    <style>
        .container {
            padding: 20px;
        }
        .top-bar {
            margin-bottom: 20px;
        }
        .add-btn {
            padding: 8px 15px;
            background-color: #1ab394;
            color: white;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }
        .add-btn:hover {
            background-color: #18a689;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            background-color: white;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
            margin-bottom: 20px;
        }
        th, td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid #e7eaec;
        }
        th {
            background-color: #f5f5f6;
            color: #333;
        }
        .action-btn {
            padding: 5px 10px;
            margin-right: 5px;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }
        .edit-btn {
            background-color: #f8ac59;
            color: white;
        }
        .edit-btn:hover {
            background-color: #f7a54a;
        }
        .delete-btn {
            background-color: #ed5565;
            color: white;
        }
        .delete-btn:hover {
            background-color: #ec4758;
        }
        .status-badge {
            padding: 3px 8px;
            border-radius: 3px;
            font-size: 12px;
        }
        .status-active {
            background-color: #1ab394;
            color: white;
        }
        .status-inactive {
            background-color: #ed5565;
            color: white;
        }
        /* 分页样式 */
        .pagination {
            display: flex;
            justify-content: center;
            align-items: center;
            margin-top: 20px;
        }
        .pagination button {
            margin: 0 5px;
            padding: 5px 10px;
            border: 1px solid #ddd;
            background-color: white;
            color: #333;
            cursor: pointer;
            border-radius: 3px;
        }
        .pagination button:hover:not(:disabled) {
            background-color: #f5f5f5;
        }
        .pagination button.active {
            background-color: #1ab394;
            color: white;
            border-color: #1ab394;
        }
        .pagination button:disabled {
            background-color: #f5f5f5;
            color: #999;
            cursor: not-allowed;
        }
        .pagination-info {
            margin: 0 15px;
            color: #666;
        }
        .search-box {
            display: inline-block;
            margin-left: 20px;
        }
        .search-box input {
            padding: 7px 10px;
            border: 1px solid #ddd;
            border-radius: 3px;
            width: 200px;
        }
        .search-btn {
            padding: 8px 15px;
            background-color: #1c84c6;
            color: white;
            border: none;
            border-radius: 3px;
            cursor: pointer;
            margin-left: 5px;
        }
        .search-btn:hover {
            background-color: #1a7bb9;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="top-bar">
            <button class="add-btn" onclick="location.href='${pageContext.request.contextPath}/admin/dish/get'">添加菜品</button>
            <div class="search-box">
                <input type="text" id="searchInput" placeholder="输入菜品名称搜索" value="${dishName}">
                <button class="search-btn" onclick="search()">搜索</button>
            </div>
        </div>
        
        <table>
            <thead>
                <tr>
                    <th>ID</th>
                    <th>图片</th>
                    <th>菜品名称</th>
                    <th>价格</th>
                    <th>状态</th>
                    <th>创建时间</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <c:forEach items="${dishes}" var="dish">
                    <tr>
                        <td>${dish.dishId}</td>
                        <td>
                            <c:if test="${not empty dish.imageUrl}">
                                <img src="${pageContext.request.contextPath}/${dish.imageUrl}" alt="${dish.dishName}" style="width: 50px; height: 50px;">
                            </c:if>
                        </td>
                        <td>${dish.dishName}</td>
                        <td>￥${dish.price}</td>
                        <td>
                            <span class="status-badge ${dish.status == 1 ? 'status-active' : 'status-inactive'}">
                                ${dish.status == 1 ? '在售' : '下架'}
                            </span>
                        </td>
                        <td><fmt:formatDate value="${dish.createTime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
                        <td>
                            <button class="action-btn edit-btn" onclick="location.href='${pageContext.request.contextPath}/admin/dish/get?id=${dish.dishId}'">编辑</button>
                            <button class="action-btn delete-btn" onclick="deleteDish(${dish.dishId})">删除</button>
                        </td>
                    </tr>
                </c:forEach>
            </tbody>
        </table>

        <!-- 分页组件 -->
        <div class="pagination">
            <button onclick="goToPage(${currentPage - 1})" ${currentPage <= 1 ? 'disabled' : ''}>
                上一页
            </button>
            
            <c:forEach begin="1" end="${totalPages}" var="i">
                <c:choose>
                    <c:when test="${i == currentPage}">
                        <button class="active">${i}</button>
                    </c:when>
                    <c:otherwise>
                        <button onclick="goToPage(${i})">${i}</button>
                    </c:otherwise>
                </c:choose>
            </c:forEach>
            
            <button onclick="goToPage(${currentPage + 1})" ${currentPage >= totalPages ? 'disabled' : ''}>
                下一页
            </button>
            
            <span class="pagination-info">
                共 ${totalCount} 条记录，${totalPages} 页
            </span>
        </div>
    </div>
    
    <script>
        function deleteDish(id) {
            if (confirm('确定要删除这个菜品吗？')) {
                fetch('${pageContext.request.contextPath}/admin/dish/delete?id=' + id, {
                    method: 'POST'
                }).then(response => response.json())
                  .then(data => {
                      if (data.success) {
                          alert('删除成功');
                          location.reload();
                      } else {
                          alert(data.message || '删除失败');
                      }
                  })
                  .catch(error => {
                      console.error('Error:', error);
                      alert('删除失败');
                  });
            }
        }

        function search() {
            const searchValue = document.getElementById('searchInput').value.trim();
            location.href = '${pageContext.request.contextPath}/admin/dish/list?dishName=' + encodeURIComponent(searchValue);
        }

        document.getElementById('searchInput').addEventListener('keypress', function(e) {
            if (e.key === 'Enter') {
                search();
            }
        });

        function goToPage(page) {
            const searchValue = document.getElementById('searchInput').value.trim();
            const searchParam = searchValue ? '&dishName=' + encodeURIComponent(searchValue) : '';
            location.href = '${pageContext.request.contextPath}/admin/dish/list?page=' + page + searchParam;
        }
    </script>
</body>
</html> 